package springboot.study.project.service.impl;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springboot.study.project.dto.BulletinDto;
import springboot.study.project.dto.DormitoryDto;
import springboot.study.project.mapper.DormitoryMapper;
import springboot.study.project.mapper.StudentMapper;
import springboot.study.project.mapper.UserMapper;
import springboot.study.project.model.*;
import springboot.study.project.service.DormitoryService;
import springboot.study.project.util.common.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: DormitoryServiceImpl
 * @Description: 宿舍相关接口实现类
 * @Author: liu-hao
 * @Date: 2021-04-12 14:29
 * @Version: 1.0
 **/
@Service
public class DormitoryServiceImpl implements DormitoryService {

    @Autowired
    DormitoryMapper dormitoryMapper;
    @Autowired
    StudentMapper studentMapper;

    @Override
    public boolean addDormitory(Dormitory dormitory) {
        if (dormitory != null && StringUtil.isNotBlank(dormitory.getDormitoryId()) && StringUtil.isNotBlank(dormitory.getDormitoryName())) {
            int insertCount = dormitoryMapper.insert(dormitory);
            return insertCount > 0 ? true : false;
        }
        return false;
    }

    @Override
    public boolean removeDormitory(String dormitoryId) {
        if (StringUtil.isNotBlank(dormitoryId)) {
            int deleteCount = dormitoryMapper.deleteByPrimaryKey(dormitoryId);
            return deleteCount > 0 ? true : false;
        }
        return false;
    }

    @Override
    public boolean modifyDormitory(Dormitory dormitory) {
        if (StringUtil.isNotBlank(dormitory.getDormitoryId())) {
            int updateCount = dormitoryMapper.updateByPrimaryKeySelective(dormitory);
            return updateCount > 0 ? true : false;
        }
        return false;
    }

    @Override
    public Dormitory findDormitoryById(String dormitoryId) {
        if (StringUtil.isNotBlank(dormitoryId)) {
            Dormitory dormitory = dormitoryMapper.selectByPrimaryKey(dormitoryId);
            return dormitory;
        } else {
            return null;
        }
    }

    @Override
    public List<Dormitory> getAllDormitory(String dormitoryId) {
        DormitoryExample example = new DormitoryExample();
        if (StringUtil.isNotBlank(dormitoryId)) {
            example.createCriteria().andDormitoryIdEqualTo(dormitoryId);
        }
        List<Dormitory> dormitories = dormitoryMapper.selectByExample(example);
        return dormitories;
    }

    @Override
    public Map<String, Object> findDormitory(String dormitoryName, int offset, int pageSize) {
        DormitoryExample example = new DormitoryExample();
        if (StringUtil.isNotBlank(dormitoryName)) {
            example.createCriteria().andDormitoryNameLike("%" + dormitoryName + "%");
        }
        // 查询符合条件宿舍数量
        int totalCount = dormitoryMapper.countByExample(example);
        // 查询符合条件的宿舍信息
        List<Dormitory> dormitories = dormitoryMapper.selectByExampleWithRowbounds(example, new RowBounds(offset, pageSize));

        // 填充宿舍属性
        List<DormitoryDto> dormitoryDtos = new ArrayList<>();
        for (Dormitory dormitory : dormitories) {
            StudentExample studentExample = new StudentExample();
            studentExample.createCriteria().andDormitoryIdEqualTo(dormitory.getDormitoryId());
            List<Student> students = studentMapper.selectByExample(studentExample);
            String memberStr = String.join("、", students.stream().map(student -> student.getStuName()).collect(Collectors.toList()));

            DormitoryDto dormitoryDto = new DormitoryDto();
            BeanUtils.copyProperties(dormitory, dormitoryDto);
            dormitoryDto.setDormitoryMember(memberStr);
            dormitoryDtos.add(dormitoryDto);
        }

        // 定义返回Map
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", 0);
        resultMap.put("count", totalCount);
        resultMap.put("data", dormitoryDtos);
        return resultMap;
    }
}
